/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.fandi.controller;

import com.fandi.model.CategoryModel;
import com.fandi.model.CategoryTableModel;
import com.fandi.util.Message;
import com.fandi.view.CategoryFrame;
import com.fandi.view.ItemFrame;
import java.sql.SQLException;
import java.util.List;

/**
 *
 * @author Fandy
 */
public class CategoryController {
    private CategoryModel model;
    private CategoryTableModel tableModel;
    
    // For update data purpose
    private boolean isUpdate;
    private int index;
    private String code;

    private ItemController itemController;
    
    public void setModel(CategoryModel model) {
        this.model = model;
    }

    public void setTableModel(CategoryTableModel tableModel) {
        this.tableModel = tableModel;
    }

    public void setItemController(ItemController itemController) {
        this.itemController = itemController;
    }
    
    public void insert(CategoryFrame view) {
        String code     = view.getTxtCatCode().getText();
        String name     = view.getTxtCatName().getText();
        String desc     = view.getTxtCatDesc().getText();
        
        try {
            if (code.equals("")) {
                Message.showMessage("Code is required", "Error", Message.ERROR);
            } else if (name.equals("")) {
                Message.showMessage("Name is required", "Error", Message.ERROR);
            } else {
                model.setCode(code);
                model.setName(name);
                model.setDescription(desc);

                
                if(isUpdate) {
                    model.update(code);
                } else {
                    model.insert();
                }
                
                Message.showMessage("Successfully saved!", "Information", Message.INFO);
                
                renderTableData(view);
                model.reset();
                
                view.getBtnRemove().setEnabled(false);
                view.getTblCatData().clearSelection();
                
                itemController.populateCategory(ItemFrame.getInstance());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        
    }
    
    public void reset(CategoryFrame view) {
        model.reset();
        
        isUpdate = false;
        view.getBtnRemove().setEnabled(false);
        view.getTblCatData().clearSelection();
    }

    public void renderTableData(CategoryFrame view) {
        try {
            List<CategoryModel> list = model.selectAll();
            tableModel.setList(list);
            view.getTblCatData().setModel(tableModel);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    public void selectItem(CategoryFrame view) {
        try {
            index   = view.getTblCatData().getSelectedRow();
            code    = (String) view.getTblCatData().getValueAt(index, 0);
            CategoryModel data = model.selectById(code);
            
            model.setId(data.getId());
            model.setCode(data.getCode());
            model.setName(data.getName());
            model.setDescription(data.getDescription());
            
            isUpdate = true;
            view.getBtnRemove().setEnabled(true);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
    public void remove(CategoryFrame view) {
        try {
            int index   = view.getTblCatData().getSelectedRow();
            String code = (String) view.getTblCatData().getValueAt(index, 0);
            
            if (model.delete(code)) {
                Message.showMessage("Successfully deleted!", "Success", Message.INFO);
                renderTableData(view);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
}
